﻿using DotNetExtensions.Core.MVC;
using DotNetExtensions.Core.WebApi.Jwt;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreWebSimple.JWT
{
    public class JWTAuthorizationActionFilter : IAsyncActionFilter
    {
        private readonly IMemoryCache _memoryCache;
        private readonly Session _session;
        public JWTAuthorizationActionFilter(IMemoryCache memoryCache, Session session)
        {
            _memoryCache = memoryCache;
            _session = session;
        }
        public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            string sid = context.HttpContext.User.Claims.First(x => x.Type == JwtParameters.Sid).Value;
            _session.User = _memoryCache.Get(sid);
            return next.Invoke();
        }
    }
}
